/*
 * Table.h      
 */

#ifndef TABLE_H_
#define TABLE_H_

#include <iostream>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
//   Creating a NODE Structure
struct row {
	string filename;
	string data;
	struct row *next;
};


class table {
	struct row *front, *rear,*cur;
public:
	table() // constructure
	{
		front = rear = NULL;
	}
	bool delet(string value);
	void insert(string value, string data); // to insert an element
	string search(string fname); // to delete an element
	void show(); // to show the stack
	string getsearch(string fname);

};
// Insertion
bool table::delet(string value){
	cur=front;
	struct row *prev,*next;
	prev=cur;

	while(cur){
		next=cur->next;
		if(cur->filename==value){
			prev->next=next;
			delete cur;
			return true;
		}
		cur=cur->next;
		prev=cur;
	}
	return false;

}
void table::insert(string value, string data) {

	struct row *ptr;
	ptr = new row;
	ptr->filename = value;
	ptr->data = data;
	ptr->next = NULL;
	if (front == NULL)
		front = ptr;
	else
		rear->next = ptr;
	rear = ptr;


}

string table::getsearch(string value) {
	string result = "";

	if (front == NULL) {
		return "NULL";
	}

	struct row *temp;
	temp = front;
	while (1) {
		if (temp->filename.compare(value) == 0) {
			if (result.compare("") != 0) {
				result += ",";
				result += temp->data;
			} else {
				result += temp->data;
				break;
			}

		}
		if (temp->next != NULL)
			temp = temp->next;
		else
			break;
	}
	if(result.compare("")==0)
		result="NO";
	return result;

}

//Search
string table::search(string value) {
	string result = "";

	if (front == NULL) {
		return "NULL";
	}

	struct row *temp;
	temp = front;
	while (1) {
		if (temp->filename.compare(value) == 0) {
			if (result.compare("") != 0) {
				result += ",";
				result += temp->data;
			} else {
				result += temp->data;
			}

		}
		if (temp->next != NULL)
			temp = temp->next;
		else
			break;
	}
	if(result.compare("")==0)
		result="NO";
	return result;

}

// Show Table
void table::show() {
	struct row *ptr1 = front;
	if (front == NULL) {

		return;
	}
	cout << "\nThe table is\n";
	while (ptr1 != NULL) {
		cout << ptr1->filename << "|" << ptr1->data << " ->";
		ptr1 = ptr1->next;
	}

}

#endif /* TABLE_H_ */
